#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：zhcx-service-test 
@File    ：listing_readjust_prices.py
@Author  ：关依林
@Date    ：2024/1/22 14:20 
'''

from testcases.ProductGrading_Strategy.AmazonListing.controller.grading_base_way import *
from testcases.ProductGrading_Strategy.AmazonListing.service.listing_grading_public_way import *
from testcases.ProductGrading_Strategy.public_dispatch_way import getStrategyResult2
from testcases.java_readjust_prices_strategy.interface import *
from testcases.java_readjust_prices_strategy.readjust_prices_sql import *

'''
验证思路:
1.已来源渠道维度遍历策略数据
2.判断是否指定渠道SKU字段,指定调用明细API,不指定调用策略报告API
3.获取到匹配的数据先过滤部门
4.计算出匹配数据(计算器价格*百分比)并过滤条件
5.查是否存在调价记录,间隔时间内是否被执行
—————————————————————以上步骤已获取匹配的数据—————————————————————————————
校验结果:
1.调整原价，是否在计算器价格的百分比内
2.
'''

def strategy_data_verify():
    strategyId = 396
    cursorListing = connectMysql("Java_strategyListing_Test")
    cursorData = connectMysql("Java_strategyData_Test")
    cursorStrategy = connectMysql("Java_centerStrategy_Test")
    orderSourceIdList = getCountryOrderSourceId(cursorListing)
    country = 'US'
    # 获取策略条件
    strategyCondition = getStrategyCondition(strategyId,cursorStrategy)
    # 判断是否指定渠道SKU字段
    assignOrderSourceSKUData = getAssignOrderSourceSKU(strategyId, cursorStrategy)
    # 判断是否有指定策略和否定策略
    assignStrategySet = getAssignStrategy(strategyId, cursorStrategy)
    relationStrategyIds, reRelationStrategyIds = (assignStrategySet.get("relation_strategy_ids"),
                                                  assignStrategySet.get("re_relation_strategy_ids"))
    for sourceId in orderSourceIdList:
        ids = getListingBaseId(sourceId)
        tempSet = []
        if assignOrderSourceSKUData:
            appointType, appointSkuSet = (assignOrderSourceSKUData[0]["appoint_type"],
                                          json.loads(assignOrderSourceSKUData[0]["appoints"]))
            idSet = getAssignListingId(ids, cursorListing, appointType, appointSkuSet)
            if relationStrategyIds:
                for id in relationStrategyIds:
                    listingIdSet = getStrategyData(cursorStrategy, int(id), sourceId)
                    idSet = list(filter(lambda x: x in listingIdSet, idSet))
                    tempSet += idSet
            # 反向
            if reRelationStrategyIds:
                for id in reRelationStrategyIds:
                    pass
            else: tempSet = idSet
        else:
            for id in relationStrategyIds:
                idSet = getStrategyData(cursorStrategy, int(id), sourceId)
                tempSet += idSet
        listingIdSet = tempSet

        # 获取指定部门
        assignDepartment = eval(getAssignDepartment(strategyId, cursorStrategy))
        # 获取匹配数据的上架人员ID
        ListingPersonIdSet = getDataListingPersonId(ids, listingIdSet, cursorListing)
        # 过滤部门数据
        idSet = getListingPersonDepartment(ListingPersonIdSet, cursorData, assignDepartment)
        #分批通过ListingId获取渠道SKU
        for idList in range(0, len(idSet), 50):
            num = idSet[idList: idList + 50]
            data = getOrderSourceSKU(ids, cursorListing, num)
            fbmSet = list(filter(lambda x: x["delivery_type"] == 0, data))
            fbaSet = list(filter(lambda x: x["delivery_type"] == 1, data))
            if fbmSet:
                fbmSet = getFbmPrice(fbmSet)
                fbmSet = CalculatorPriceAPI(fbmSet, country, sourceId,businessTypeCode='AmazonSelfDeliveryLocalWarehouse')
            if fbaSet:
                fbaSet = getFbaPrice(fbaSet, sourceId)
                fbaSet = CalculatorPriceAPI(fbaSet,country,sourceId,businessTypeCode='AmzonFBANormal')
            if strategyCondition:
                for condition in strategyCondition:
                    condition, conditionValue = condition["condition_field"], condition["condition_value"]

                    if condition == "listing_price":
                        rare = conditionValue["rate"]
                        conditionalOperator = conditionValue["conditionalOperator"]
                        listingPriceType = "listing_price"
                        if conditionValue["listingPriceType"] == 2:
                            listingPriceType = "discount_price"
                        if conditionalOperator == ">":
                            fbmSet = list(filter(lambda x: x[listingPriceType] > x["price"] * rare * 0.01, fbmSet))
                            fbaSet = list(filter(lambda x: x[listingPriceType] > x["price"] * rare * 0.01, fbaSet))

                    if condition == "manual_adjustment":
                        cursorStrategyAmazon = connectMysql("Java_strategyAmazon_Test")
                        days, listingPriceType = conditionValue["days"], conditionValue["listingPriceType"]
                        date = (datetime.datetime.now() + datetime.timedelta(days=-days)).strftime("%Y-%m-%d")
                        date = datetime.datetime.strptime(date + ' 00:00:00', '%Y-%m-%d %H:%M:%S')
                        if listingPriceType == 1: listingPriceType = 2
                        else: listingPriceType = 3
                        tempSet = list(filter(lambda x:x["type"] == listingPriceType and x["create_time"] >= date, fbmSet))
                        fbmSet = list(filter(lambda x:x["msku"] not in tempSet, fbmSet))
                        fbaSet = getQueueData(fbaSet,sourceId,cursorStrategyAmazon)

                    if condition == "interval_days":
                        pass
                    if condition == "number_of_runs":
                        pass
                    if condition == "adjust_original_price":
                        pass
                    if condition == "same_country_asin":
                        pass
                    if condition == "mantissa":
                        pass








if __name__ == '__main__':
    strategy_data_verify()

